﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;

namespace ECommerce.Model.Controles
{
    public class WebControl : ECommerce.Model.Objeto
    {
        List<System.Reflection.EventInfo> eventsInfo;
        public System.Reflection.EventInfo[] EventosInfo
        { get { return this.eventsInfo.ToArray(); } }
        private ECommerce.Model.Controles.WebControl[] controles;
        public ECommerce.Model.Controles.WebControl[] Controles
        { get { return this.controles; } set { this.controles = value; } }
        private string imageUrl;
        public string ImageUrl
        { get { return this.imageUrl; } set { this.imageUrl = value; } }
        private string commandArgument;
        public string CommandArgument
        { get { return this.commandArgument; } set { this.commandArgument = value; } }
        public event System.Web.UI.WebControls.CommandEventHandler Command;
        private System.Type tipo;
        public System.Type Tipo
        { get { return this.tipo; } set { this.tipo = value; } }
        public System.Web.UI.WebControls.WebControl webControl
        {
            get
            {
                System.Web.UI.WebControls.WebControl webControl = (System.Web.UI.WebControls.WebControl)this.Tipo.Assembly.CreateInstance(this.Tipo.FullName);
                webControl.ID = this.Denominación;
                if (Controles != null)
                {
                    foreach (ECommerce.Model.Controles.WebControl Ctrl in Controles)
                    {
                        webControl.Controls.Add(Ctrl.webControl);
                    }
                }
                return ECommerce.Reflection.Tipos.GetWebControl(webControl, this.Tipo, new System.Web.UI.WebControls.CommandEventHandler(lnkAdd_Command), this.CommandArgument, this.ImageUrl);
            }
        }
        void lnkAdd_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
        { Command(sender, e); }

        public override void Load(int ident)
        {
            base.Load(ident);
            ECommerce.Model.Controles.WebControl webControl = ECommerce.DAO.Controles.WebControl.GetWebControlById(ident);
            this.Tipo = webControl.tipo;
            this.Controles = ECommerce.DAO.Controles.WebControl.GetWebControlsByWebControlId(this.Id);
        }
        public override void Load(System.Data.DataRow dr)
        {
            base.Load(dr);
            ECommerce.Factory.Controles.WebControl.Load(this,dr);
        }
        public override void Save()
        {
            base.Save();
            this.Id = ECommerce.DAO.SQL.Controles.WebControl.SetWebControl(this);
            ECommerce.DAO.Cache.Controles.WebControl.SetWebControl(this);
        }
        public override string ToString()
        { return this.Tipo.ToString() + "." + this.Denominación; }
        public override void Del()
        {
            base.Del();
            ECommerce.DAO.Cache.Controles.WebControl.DelWebControl(this);
        }
        public override string NavigateUrl
        { get { return "/Edición/Control.aspx?Id=" + this.Id; } }

        public void Add(WebControl WebControl)
        {
            ECommerce.DAO.SQL.Controles.WebControl.AddWebControlToWebControl(WebControl,this);
            ECommerce.DAO.Cache.Controles.WebControl.DelWebControlsByWebControlId(WebControl.Id);
        }
    }
}
